home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2002 #7 / K-CD-7-2002.ISO / Xara Webstyle 2.1 / WebStyleDwExt.cab / data1.cab / CfgUdMenuMgr / WSDwExMenuMgr.js < prev   
Encoding:
JavaScript  |  2002-01-09  |  5.4 KB  |  198 lines

  1. //*************** GLOBALS  *****************
  2.  
  3. var Xara_NEWLINE = (navigator.platform != "Win32") ? "\x0D" : "\x0D\x0A";
  4. var Xara_FILE_configPath = dw.getConfigurationPath();
  5. var Xara_FILE_menus = Xara_FILE_configPath + "/Menus/menus.xml";
  6. var Xara_FILE_menusBackup = Xara_FILE_configPath + "/Menus/menus.xml.xara";
  7.  
  8.  
  9. function XaraFindMenu(XMLDOM, menuId, menuBarId)
  10. {
  11.     var retVal = null;
  12.     var menuBarList = XMLDOM.getElementsByTagName("MENUBAR");
  13.     var menuBar = '';
  14.  
  15.     for (var i = 0; i < menuBarList.length; i++)
  16.     {
  17.         if (menuBarList[i].getAttribute("ID") == menuBarId) { menuBar = menuBarList[i]; break; }
  18.     }
  19.  
  20.     if (!menuBar) { return retVal; }
  21.     var menuList = menuBar.getElementsByTagName("MENU");
  22.     for (i = 0; i < menuList.length; i++)
  23.     {
  24.         if (menuList[i].getAttribute("ID") == menuId) { retVal = menuList[i]; break; }
  25.     }
  26.     
  27.     return retVal;
  28. }
  29.  
  30. function XaraFindMenuItem(menu, itemId)
  31. {
  32.     var retVal = null;
  33.     var menuItemList = menu.getElementsByTagName("MENUITEM");
  34.  
  35.     for (i = 0; i < menuItemList.length; i++)
  36.     {
  37.         if (menuItemList[i].getAttribute("ID") == itemId) { retVal = menuItemList[i]; break; }
  38.     }
  39.  
  40.     if (null == retVal)
  41.     {
  42.         var separatorList = menu.getElementsByTagName("SEPARATOR");
  43.         for (i = 0; i < separatorList.length; i++)
  44.         {
  45.             if (separatorList[i].getAttribute("REFERENCES") == itemId) { retVal = separatorList[i]; break; }
  46.         }
  47.     }
  48.  
  49.     return retVal;
  50. }
  51.  
  52. function XaraUpdateMenus(XMLDOM, menuOriginal)
  53. {
  54.     var retVal = true;
  55.  
  56.     // backup menus.xml
  57.     DWfile.write(Xara_FILE_menusBackup, menuOriginal.body.innerHTML);
  58.  
  59.     if (!DWfile.write(Xara_FILE_menus, XMLDOM.body.innerHTML))
  60.     {
  61.         retVal = false;
  62.         alert(MSG_menuFileUpdateFailed.replace(/%s/, MMNotes.localURLToFilePath(FILE_menus)));
  63.     }
  64.  
  65.     return retVal;
  66. }
  67.  
  68. function XaraManageMenus()
  69. {
  70.     var retVal = true;
  71.     var bUpdateMenus = false;
  72.     var bMenuOpFailed = false;
  73.  
  74.     XaraTestFile = Xara_FILE_configPath + XaraTestFile;
  75.     
  76.     // Is the extension installed?
  77.     var bInstalled    = DWfile.exists(XaraTestFile);
  78.  
  79.     // Load Menus\menus.xml
  80.     var menuDOM = dw.getDocumentDOM(Xara_FILE_menus);
  81.     var originalMenuDOM = menuDOM;
  82.  
  83.     // Is the menu present?
  84.     if (menuDOM.body.innerHTML.indexOf(XaraMenuId) == (-1))
  85.     {
  86.         // No. Is the extension installed?
  87.         if (bInstalled)
  88.         {
  89.             // Add the insert menu items...
  90.             bMenuOpFailed |= !XaraAddXML(menuDOM, XaraParentMenuId, XaraInsertAfter, XaraMenu, XaraMenuError, "DWMainWindow");
  91.  
  92.             // Add the text context menu item...
  93.             bMenuOpFailed |= !XaraAddXML(menuDOM, XaraContextMenuTextParentId, XaraContextMenuTextInsertAfter, XaraContextMenuText, XaraMenuError, "DWTextContext");
  94.  
  95.             // Add the image context menu item...
  96.             bMenuOpFailed |= !XaraAddXML(menuDOM, XaraContextMenuImageParentId, XaraContextMenuImageInsertAfter, XaraContextMenuImage, XaraMenuError, "DWImageContext");
  97.  
  98.             bUpdateMenus = true;
  99.         }
  100.     }
  101.     else
  102.     {
  103.         // The menu is present - Has the extension been removed?
  104.         if (!bInstalled)
  105.         {
  106.             // Yes, remove the Xara menu entries
  107.             bMenuOpFailed |= !XaraRemoveXML(menuDOM, XaraMenu, XaraMenuError);
  108.  
  109.             // Remove the text context menu item...
  110.             bMenuOpFailed |= !XaraRemoveXML(menuDOM, XaraContextMenuText, XaraMenuError);
  111.  
  112.             // Remove the image context menu item...
  113.             bMenuOpFailed |= !XaraRemoveXML(menuDOM, XaraContextMenuImage, XaraMenuError);
  114.  
  115.             bUpdateMenus = true;
  116.         }
  117.     }
  118.  
  119.     if (bUpdateMenus && !bMenuOpFailed)
  120.     {
  121.         XaraUpdateMenus(menuDOM, originalMenuDOM);
  122.     }
  123.  
  124.     dw.releaseDocument(menuDOM);
  125.  
  126.     return retVal;
  127. }
  128.  
  129. function XaraAddXML(XMLDOM, parentMenuId, insertAfterId, menuXML, menuError, menuBarId)
  130. {
  131.     var retVal = true;
  132.     menuXML = menuXML.replace(/\n/g, Xara_NEWLINE);
  133.  
  134.     if (!XaraAddXMLFunction(XMLDOM, parentMenuId, insertAfterId, menuXML, menuBarId)) { retVal = false; }
  135.     if (!retVal) { alert(menuError.replace(/%s/, "add")); }
  136.     return retVal;
  137. }
  138.  
  139. function XaraAddXMLFunction(XMLDOM, parentMenuId, insertAfterId, menuXML, menuBarId)
  140. {
  141.     var parentMenu = XaraFindMenu(XMLDOM, parentMenuId, menuBarId);
  142.  
  143.     if (null == parentMenu) { return false; }
  144.  
  145.     var insertAfter = XaraFindMenuItem(parentMenu, insertAfterId);
  146.     if (null != insertAfter)
  147.     {
  148.         insertAfter.outerHTML = insertAfter.outerHTML + Xara_NEWLINE + menuXML;
  149.     }
  150.     else
  151.     {
  152.         parentMenu.innerHTML = parentMenu.innerHTML + Xara_NEWLINE + menuXML;
  153.     }
  154.  
  155.     return true;
  156. }
  157.  
  158. function XaraRemoveXML(XMLDOM, menuXML, menuError)
  159. {
  160.     var retVal = true;
  161.  
  162.     // The XML passed in may include any of the following characters which will be parsed incorrectly in a regular expression...
  163.     // Replace these with their literal forms...
  164.     menuXML = menuXML.replace(/\n */g, "\\s*");
  165.     menuXML = menuXML.replace(/\//g, "\\\/");
  166.     menuXML = menuXML.replace(/\"/g, "\\\"");
  167.     menuXML = menuXML.replace(/\./g, "\\.");
  168.     menuXML = menuXML.replace(/\)/g, "\\)");
  169.     menuXML = menuXML.replace(/\(/g, "\\(");
  170.  
  171.     if (!XaraRemoveXMLFunction(XMLDOM, menuXML)) { retVal = false; }
  172.     if (!retVal) { alert(menuError.replace(/%s/, "remove")); }
  173.     return retVal;
  174. }
  175.  
  176. function XaraRemoveXMLFunction(XMLDOM, menuXML)
  177. {
  178.     var retVal = false;
  179.     var xmlDoc = XMLDOM.body.innerHTML;
  180.  
  181.     var matchRE = new RegExp(menuXML, "gi");
  182.     matchRE.multiLine = true;
  183.     var result = matchRE.exec(xmlDoc);
  184.  
  185.     if (result != null)
  186.     {
  187.         var startIndex = result.index;
  188.         var stopIndex = matchRE.lastIndex;
  189.  
  190.         if (startIndex > (-1))
  191.         {
  192.             XMLDOM.body.innerHTML = RegExp.leftContext + RegExp.rightContext;
  193.             retVal = true;
  194.         }
  195.     }
  196.  
  197.     return retVal;
  198. }